﻿public partial class Solution
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="letters"></param>
    /// <param name="target"></param>
    /// <returns></returns>
    public char NextGreatestLetter(char[] letters, char target)
    {
        char result = letters[0];
        if (target < letters[^1])
        {
            int lp = 0;
            int rp = letters.Length - 1;
            while (lp <= rp)
            {
                int mid = (lp + rp) / 2;
                if (letters[mid] <= target)
                {
                    lp = mid + 1;
                }
                else
                {
                    rp = mid - 1;
                }
            }
            result = letters[lp];
        }
        return result;
    }
}